/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.trandio.manager;

import org.trandio.model.PlanoANVO;
import org.trandio.model.PlanoAHVO;
import org.trandio.model.FacturaVO;
import org.trandio.model.PlanoAUVO;
import org.trandio.model.DetalleFacturaVO;
import org.trandio.model.CabRipVO;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.trandio.dao.LiquidacionDAO;
import org.trandio.dao.LiquidacionInterface;
import org.trandio.dao.RipsDAO;
import org.trandio.dao.RipsInterfase;
import java.sql.Timestamp;
import java.util.Calendar;

/**
 *
 * @author root
 */
public class RipsServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    RipsInterfase ripsInterfase;
    List<DetalleFacturaVO> listDetalleFacturaVO;
    List<PlanoANVO> listaRn;
    private CabRipVO cabRipVO;
    private LiquidacionInterface liquidacionInterface;
    private FacturaVO facturaVO;
    private PlanoAHVO planoAHVO;
    private PlanoAUVO planoAUVO;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        ripsInterfase = new RipsDAO();
        liquidacionInterface = new LiquidacionDAO();


        int op = -1;


        if (request.getParameter("op") != null) {
            if (!request.getParameter("op").equals("")) {
                op = Integer.parseInt(request.getParameter("op"));
            }
        }


        int rip = 0;
        if (request.getParameter("rip") != null) {
            if (!request.getParameter("rip").equals("")) {
                rip = Integer.parseInt(request.getParameter("rip"));
            }
        }

        int tipo = -1;
        if (!request.getParameter("tipo").equals("")) {
            tipo = Integer.parseInt(request.getParameter("tipo"));
        }

        System.out.println(tipo);

        try {

            switch (op) {

                case 1:


                    int idfactura = Integer.parseInt(request.getParameter("idfactura"));
                    try {
                        listDetalleFacturaVO = ripsInterfase.getDetallesFactura(idfactura);
                    } catch (SQLException ex) {
                        Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    Iterator it = listDetalleFacturaVO.iterator();

                    String ac = "";
                    String ap = "";
                    String at = "";

                    while (it.hasNext()) {
                        DetalleFacturaVO b = (DetalleFacturaVO) it.next();

                        if (b.getTipoRegistro().equals("AC")) {
                            ac += "<tr><td>" + b.getCodigo() + "</td><td>" + b.getDescripcion() + "</td>"
                                    + "<td><img style='cursor:pointer' onclick='llenarRips(" + b.getId() + ",\"" + b.getCodigo() + "\",\"" + b.getDescripcion() + "\",\"" + b.getTipoRegistro() + "\",\"" + b.getFecha() + "\",\"" + b.getVrunitario() + "\",\"" + b.getVrpaciente() + "\",\"" + b.getVrtotal() + "\")' width='16px' height='16px' src='../common/img/asig.png' /></td>"
                                    + "</tr>";
                        }

                        if (b.getTipoRegistro().equals("AP")) {
                            ap += "<tr><td>" + b.getCodigo() + "</td><td>" + b.getDescripcion() + "</td>"
                                    + "<td><img style='cursor:pointer' onclick='llenarRips(" + b.getId() + ",\"" + b.getCodigo() + "\",\"" + b.getDescripcion() + "\",\"" + b.getTipoRegistro() + "\",\"" + b.getFecha() + "\",\"" + b.getVrunitario() + "\",\"" + b.getVrpaciente() + "\",\"" + b.getVrtotal() + "\")' width='16px' height='16px' src='../common/img/asig.png' /></td>"
                                    + "</tr>";
                        }

                        if (b.getTipoRegistro().equals("AT") || b.getTipoRegistro().equals("AM")) {
                            at += "<tr><td>" + b.getCodigo() + "</td><td>" + b.getDescripcion() + "</td>"
                                    + "<td><img style='cursor:pointer' onclick='llenarRips(" + b.getId() + ",\"" + b.getCodigo() + "\",\"" + b.getDescripcion() + "\",\"" + b.getTipoRegistro() + "\",\"" + b.getFecha() + "\",\"" + b.getVrunitario() + "\",\"" + b.getVrpaciente() + "\",\"" + b.getVrtotal() + "\")' width='16px' height='16px' src='../common/img/asig.png' /></td>"
                                    + "</tr>";
                        }
                    }

                    switch (tipo) {
                        case 0:
                            out.println("<table>" + ac + "</table>");
                            break;
                        case 1:
                            out.println("<table>" + ap + "</table>");
                            break;
                        case 2:
                            out.println("<table>" + at + "</table>");
                            break;

                        case 3:

                            String resp = "{}";

                            try {
                                facturaVO = liquidacionInterface.consultarFactura(idfactura + "");
                            } catch (SQLException ex) {
                                Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (NamingException ex) {
                                Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            Gson gson = new Gson();
                            out.println(gson.toJson(facturaVO));
                            // out.println(resp);
                            break;
                    }

                    try {
                        cabRipVO = ripsInterfase.getCabRip(idfactura);
                    } catch (SQLException ex) {
                        Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    switch (rip) {

                        case 1://almacenar el rip de consulta
                            String codprestador = cabRipVO.getCodPrestador();
                            int id_paciente = cabRipVO.getIdPaciente();
                            String numero_factura = cabRipVO.getNumFactura();
                            //String cod_actividad = request.getParameter("codactripcn");
                            String valor = request.getParameter("valorcn");
                            String valor_copago = request.getParameter("valorcopagocn");
                            String valor_total = request.getParameter("valortotalcn");
                            int band = Integer.parseInt(request.getParameter("band_rips"));
                            if (band == 1) {
                                try {

                                    ripsInterfase.guardarRipsConsulta(
                                            0,
                                            id_paciente,
                                            request.getParameter("fecharipcn"),
                                            request.getParameter("ordenripcn"),
                                            Integer.parseInt(request.getParameter("iddetallecn").trim()),
                                            Integer.parseInt(request.getParameter("finalidadcn").trim()),
                                            Integer.parseInt(request.getParameter("causaexcn").trim()),
                                            request.getParameter("id_dxpalcn"),
                                            request.getParameter("id_dxcn1"),
                                            request.getParameter("id_dxcn2"),
                                            request.getParameter("id_dxcn3"),
                                            Integer.parseInt(request.getParameter("tipodxcn").trim()),
                                            valor,
                                            valor_copago,
                                            valor_total,
                                            Integer.parseInt(request.getParameter("idedetadm").trim()));

                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            } else {
                                try {
                                    ripsInterfase.actualizarRipsConsulta(
                                            Integer.parseInt(request.getParameter("idedetadm")),
                                            Integer.parseInt(request.getParameter("finalidadcn")),
                                            request.getParameter("id_dxpalcn"),
                                            request.getParameter("id_dxcn1"),
                                            request.getParameter("id_dxcn2"),
                                            request.getParameter("id_dxcn3"),
                                            request.getParameter("ordenripcn"),
                                            Integer.parseInt(request.getParameter("tipodxcn")),
                                            Integer.parseInt(request.getParameter("causaexcn")));
                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                            break;

                        case 2://almacenar el rip de procedimiento
                            try {
                                int finPro = 0,
                                        forPro = 0,
                                        ambPro = 0,
                                        perPro = 0;

                                if (!request.getParameter("finalidadap").equals("")) {
                                    finPro = Integer.parseInt(request.getParameter("finalidadap").trim());
                                }

                                if (!request.getParameter("formaqxap").equals("")) {
                                    forPro = Integer.parseInt(request.getParameter("formaqxap").trim());
                                }

                                if (!request.getParameter("ambitoap").equals("")) {
                                    ambPro = Integer.parseInt(request.getParameter("ambitoap").trim());
                                }

                                if (!request.getParameter("personalatiendeap").equals("")) {
                                    perPro = Integer.parseInt(request.getParameter("personalatiendeap").trim());
                                }


                                //almacenar el rip de procedimiento
                                String codprestadorap = cabRipVO.getCodPrestador();
                                int id_pacienteap = cabRipVO.getIdPaciente();
                                String numero_facturaap = cabRipVO.getNumFactura();

                                String cod_actividadap = request.getParameter("codactripap");
                                String valorap = request.getParameter("valorap");


                                ripsInterfase.guardarRipsProcedimiento(
                                        0,
                                        id_pacienteap,
                                        request.getParameter("fecharipap"),
                                        request.getParameter("ordenripap"),
                                        Integer.parseInt(request.getParameter("iddetalleap").trim()),
                                        ambPro,
                                        finPro,
                                        perPro,
                                        request.getParameter("id_dxpalap"),
                                        request.getParameter("id_dxap1"),
                                        request.getParameter("id_dxapcom"),
                                        forPro,
                                        request.getParameter("valorap"),
                                        Integer.parseInt(request.getParameter("idedetadm").trim()));
                            } catch (SQLException ex) {
                                Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (NamingException ex) {
                                Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            break;



                        /*
                         * case 3: try { /* cama	2 causaexin	2 estadoin	2
                         * estadosal	1 fecin	09/02/2011 fecsal	10/02/2011 horain
                         * 02:07 horasal	04:07 id_dxest1	0 id_dxest2	0 id_dxest3
                         * 0 id_dxestm	0 id_dxin	0 id_dxsal	0 ordenripest	323
                         * rip	3 tipo	-1 viaingreso	2
                         *
                         * //almacenar rips de estancia
                         * ripsInterfase.guardarRipsEstancia(idfactura,
                         * request.getParameter("fecin") + " " +
                         * request.getParameter("horain"),
                         * request.getParameter("viaingreso"),
                         * request.getParameter("id_dxin"),
                         * request.getParameter("cama"),
                         * request.getParameter("causaexin"),
                         * request.getParameter("ordenripest"),
                         * request.getParameter("estadoin"),
                         * request.getParameter("fecsal") + " " +
                         * request.getParameter("horasal"),
                         * request.getParameter("estadosal"),
                         * request.getParameter("id_dxsal"),
                         * request.getParameter("id_dxest1"),
                         * request.getParameter("id_dxest2"),
                         * request.getParameter("id_dxest3"),
                         * request.getParameter("id_dxestm"),
                         * request.getParameter("destinosal"));
                         *
                         *
                         * } catch (SQLException ex) {
                         * Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE,
                         * null, ex); } catch (NamingException ex) {
                         * Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE,
                         * null, ex); }
                         *
                         *
                         * break;
                         */



                        case 3:

                            int viaIng = 0,
                             cauExtIng = 0,
                             estSalEgr = 0,
                             cauExtMueEgr = 0;

                            if (!request.getParameter("viaingah").equals("")) {
                                viaIng = Integer.parseInt(request.getParameter("viaingah").trim());
                            }

                            if (!request.getParameter("cauextingah").equals("")) {
                                cauExtIng = Integer.parseInt(request.getParameter("cauextingah").trim());
                            }


                            if (!request.getParameter("estsalah").equals("")) {
                                estSalEgr = Integer.parseInt(request.getParameter("estsalah").trim());
                            }
                            String codprestadorah = request.getParameter("idprestador");
                            int id_pacienteah = Integer.parseInt(request.getParameter("idpaciente"));
                            band = Integer.parseInt(request.getParameter("band_rips"));
                            String numFac = "";

                            if (band == 1) {

                                try {
                                    ripsInterfase.guardarRipsHospitalizacion(
                                            0,
                                            id_pacienteah,
                                            viaIng,
                                            request.getParameter("fecingah"),
                                            request.getParameter("numero_factura"),
                                            cauExtIng,
                                            request.getParameter("dxingah"),
                                            request.getParameter("dxsalah"),
                                            request.getParameter("dxrel1ah"),
                                            request.getParameter("dxrel2ah"),
                                            request.getParameter("dxrel3ah"),
                                            request.getParameter("dxcomah"),
                                            estSalEgr,
                                            //cauExtMueEgr,
                                            request.getParameter("dxmuesalah"),
                                            request.getParameter("fecsalah"),
                                            Integer.parseInt(request.getParameter("idadmision").trim()));

                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            } else {
                                try {
                                    ripsInterfase.actualizarRipsOtroServicio(
                                            request.getParameter("ordenripat"),
                                            0,
                                            Integer.parseInt(request.getParameter("idedetadm").trim()));

                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }



                            break;
                        case 4:

                            // GUARDAR RECIEN NACIDO

                            String codprestadorap = cabRipVO.getCodPrestador();
                            int id_pacienteap = cabRipVO.getIdPaciente();
                            String numero_facturaap = cabRipVO.getNumFactura();

                            try {
                                int n = 0;
                                //ripsInterfase.guardarRecienNacido(numFac, id_pacienteap, request.getParameter("fecrn") + " " + request.getParameter("horanac"), request.getParameter("edadges"), request.getParameter("controlpre"), request.getParameter("idsexo"), request.getParameter("peso"), request.getParameter("id_dxnac"), request.getParameter("id_dxmu"), request.getParameter("fecmu") + " " + request.getParameter("horamu"), request.getParameter("nomrn"),1);
                                ripsInterfase.guardarRipsRecienNacido(numero_facturaap,
                                        id_pacienteap,
                                        request.getParameter("fecrn"),
                                        request.getParameter("edadges"),
                                        request.getParameter("controlpre"),
                                        request.getParameter("idsexo"),
                                        request.getParameter("peso"),
                                        request.getParameter("id_dxnac"),
                                        request.getParameter("id_dxmu"),
                                        request.getParameter("fecmu"),
                                        request.getParameter("nomrn"),
                                        Integer.parseInt(request.getParameter("id_detalle_admision").trim()));
                            } catch (SQLException ex) {
                                Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (NamingException ex) {
                                Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                            }


                            try {
                                listaRn = ripsInterfase.listaRecienNacido(idfactura);
                            } catch (SQLException ex) {
                                Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (NamingException ex) {
                                Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            Iterator itrn = listaRn.iterator();
                            out.println("<table width='100%' style='border:1px solid gray' class='extdt-caption'>");
                            out.println("<tr class='ui-widget-header'><td colspan='4'>Recien Nacidos</td></tr>");
                            out.println("<tr class='ui-widget-header'><td>Nombre</td><td>Fecha Nac</td><td>Sexo</td><td>Peso</td></tr>");
                            while (itrn.hasNext()) {
                                PlanoANVO b = (PlanoANVO) itrn.next();
                                out.println("<tr><td>" + b.getNom() + "</td><td>" + b.getFechaNacimiento() + "</td><td>" + b.getSexo() + "</td><td>" + b.getPeso() + "</td></tr>");

                            }
                            out.println("</table>");
                            break;
                        case 5:
                            /*
                             * LISTA RECIEN NACIDOS
                             */
                            /*
                             * try { listaRn =
                             * ripsInterfase.listaRecienNacido(idfactura); }
                             * catch (SQLException ex) {
                             * Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE,
                             * null, ex); } catch (NamingException ex) {
                             * Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE,
                             * null, ex); }
                             *
                             *
                             * Iterator itrn = listaRn.iterator();
                             *
                             *
                             * out.println("<table width='100%'
                             * style='border:1px solid gray'
                             * class='extdt-caption'>"); out.println("<tr
                             * class='ui-widget-header'><td colspan='4'>Recien
                             * Nacidos</td></tr>"); out.println("<tr
                             * class='ui-widget-header'><td>Nombre</td><td>Fecha
                             * Nac</td><td>Sexo</td><td>Peso</td></tr>");
                             * while(itrn.hasNext()) { PlanoANVO b = (PlanoANVO)
                             * itrn.next();
                             * out.println("<tr><td>"+b.getNom()+"</td><td>"+b.getFechaNacimiento()+"</td><td>"+b.getSexo()+"</td><td>"+b.getPeso()+"</td></tr>");
                             *
                             * }
                             * out.println("</table>");
                             */
                            break;

                    }

                    break;

                case 2:

                    String numfac = "0";

                    switch (rip) {

                        case 1://almacenar el rip de consulta a la admision                            

                            int finCon = 0,
                             cauExt = 0,
                             tipodx = 0;

                            if (!request.getParameter("finalidadcn").equals("")) {
                                finCon = Integer.parseInt(request.getParameter("finalidadcn").trim());
                            }

                            if (!request.getParameter("causaexcn").equals("")) {
                                cauExt = Integer.parseInt(request.getParameter("causaexcn").trim());
                            }

                            if (!request.getParameter("tipodxcn").equals("")) {
                                tipodx = Integer.parseInt(request.getParameter("tipodxcn").trim());
                            }

                            //String codprestador = request.getParameter("idprestador");

                            int id_paciente = Integer.parseInt(request.getParameter("idpaciente"));

                            //String numero_factura = cabRipVO.getNumFactura();

                            // String cod_actividad = request.getParameter("codactripcn");

                            String valor = request.getParameter("valorcn");
                            String valor_copago = request.getParameter("valorcopagocn");
                            String valor_total = request.getParameter("valortotalcn");

                            int band = Integer.parseInt(request.getParameter("band_rips"));

                            if (band == 1) {

                                try {

                                    ripsInterfase.guardarRipsConsulta(
                                            0,
                                            id_paciente,
                                            request.getParameter("fecharipcn"),
                                            request.getParameter("ordenripcn"),
                                            Integer.parseInt(request.getParameter("iddetallecn").trim()),
                                            finCon,
                                            cauExt,
                                            request.getParameter("id_dxpalcn"),
                                            request.getParameter("id_dxcn1"),
                                            request.getParameter("id_dxcn2"),
                                            request.getParameter("id_dxcn3"),
                                            tipodx,
                                            valor,
                                            valor_copago,
                                            valor_total,
                                            Integer.parseInt(request.getParameter("idedetadm").trim()));

                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            } else {
                                try {
                                    ripsInterfase.actualizarRipsConsulta(
                                            Integer.parseInt(request.getParameter("idedetadm").trim()),
                                            finCon,
                                            request.getParameter("id_dxpalcn"),
                                            request.getParameter("id_dxcn1"),
                                            request.getParameter("id_dxcn2"),
                                            request.getParameter("id_dxcn3"),
                                            request.getParameter("ordenripcn"),
                                            tipodx,
                                            cauExt);
                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                            break;

                        case 2://almacenar el rip de procedimiento de la admision   

                            int finPro = 0,
                             forPro = 0,
                             ambPro = 0,
                             perPro = 0;

                            if (!request.getParameter("finalidadap").equals("")) {
                                finPro = Integer.parseInt(request.getParameter("finalidadap").trim());
                            }

                            if (!request.getParameter("formaqxap").equals("")) {
                                forPro = Integer.parseInt(request.getParameter("formaqxap").trim());
                            }

                            if (!request.getParameter("ambitoap").equals("")) {
                                ambPro = Integer.parseInt(request.getParameter("ambitoap").trim());
                            }

                            if (!request.getParameter("personalatiendeap").equals("")) {
                                perPro = Integer.parseInt(request.getParameter("personalatiendeap").trim());
                            }

                            //String codprestadorap = request.getParameter("idprestador");

                            int id_pacienteap = Integer.parseInt(request.getParameter("idpaciente"));


                            band = Integer.parseInt(request.getParameter("band_rips"));

                            if (band == 1) {
                                try {
                                    ripsInterfase.guardarRipsProcedimiento(
                                            0,
                                            id_pacienteap,
                                            request.getParameter("fecharipap"),
                                            request.getParameter("ordenripap"),
                                            Integer.parseInt(request.getParameter("iddetalleap").trim()),
                                            ambPro,
                                            finPro,
                                            perPro,
                                            request.getParameter("id_dxpalap"),
                                            request.getParameter("id_dxap1"),
                                            request.getParameter("id_dxapcom"),
                                            forPro,
                                            request.getParameter("valorap"),
                                            Integer.parseInt(request.getParameter("idedetadm").trim()));

                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            } else {
                                try {
                                    ripsInterfase.actualizarRipsProcedimiento(
                                            request.getParameter("ordenripap"),
                                            ambPro,
                                            finPro,
                                            perPro,
                                            request.getParameter("id_dxpalap"),
                                            request.getParameter("id_dxap1"),
                                            request.getParameter("id_dxapcom"),
                                            forPro,
                                            Integer.parseInt(request.getParameter("idedetadm").trim()));

                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }

                            break;

                        case 3://almacenar el rip de otro servicio   

                            int tipSer = 0;

                            if (!request.getParameter("tiposerat").equals("")) {
                                tipSer = Integer.parseInt(request.getParameter("tiposerat").trim());
                            }

                            //String codprestadorat = request.getParameter("idprestador");

                            int id_pacienteat = Integer.parseInt(request.getParameter("idpaciente"));


                            band = Integer.parseInt(request.getParameter("band_rips"));

                            if (band == 1) {

                                try {
                                    ripsInterfase.guardarRipsOtroServicio(
                                            0,
                                            id_pacienteat,
                                            request.getParameter("fecharipat"),
                                            request.getParameter("ordenripat"),
                                            Integer.parseInt(request.getParameter("iddetalleat").trim()),
                                            tipSer,
                                            Integer.parseInt(request.getParameter("cantidadat")),
                                            request.getParameter("valorat"),
                                            request.getParameter("valortotalat"),
                                            Integer.parseInt(request.getParameter("idedetadm").trim()));

                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            } else {
                                try {
                                    ripsInterfase.actualizarRipsOtroServicio(
                                            request.getParameter("ordenripat"),
                                            tipSer,
                                            Integer.parseInt(request.getParameter("idedetadm").trim()));

                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }

                            break;

                        case 4://almacenar el rip medicamento   

                            String codprestadoram = request.getParameter("idprestador");

                            int id_pacienteam = Integer.parseInt(request.getParameter("id_paciente").trim());

                            band = Integer.parseInt(request.getParameter("band_rips"));

                            if (band == 1) {

                                try {
                                    ripsInterfase.guardarRipsMedicamento(
                                            0,
                                            id_pacienteam,
                                            request.getParameter("fecha"),
                                            request.getParameter("numero_orden"),
                                            Integer.parseInt(request.getParameter("iddetalleam").trim()),
                                            Integer.parseInt(request.getParameter("numero_unidades").trim()),
                                            request.getParameter("valor_unitario"),
                                            request.getParameter("valor_total"),
                                            Integer.parseInt(request.getParameter("id_detalle_admision").trim()));

                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            } else {
                                try {
                                    ripsInterfase.actualizarRipsMedicamento(
                                            request.getParameter("numero_orden"),
                                            Integer.parseInt(request.getParameter("id_detalle_admision").trim()));

                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }

                            break;

                        case 5://almacenar el rip de hospitalizacion   

                            int viaIng = 0,
                             cauExtIng = 0,
                             estSalEgr = 0,
                             cauExtMueEgr = 0,
                             ideRes = 0;

                            if (!request.getParameter("viaingah").equals("")) {
                                viaIng = Integer.parseInt(request.getParameter("viaingah").trim());
                            }

                            if (!request.getParameter("cauextingah").equals("")) {
                                cauExtIng = Integer.parseInt(request.getParameter("cauextingah").trim());
                            }

                            if (!request.getParameter("estsalah").equals("")) {
                                estSalEgr = Integer.parseInt(request.getParameter("estsalah").trim());
                            }

                            /*
                             * if
                             * (!request.getParameter("responsableingah").equals(""))
                             * { ideRes =
                             * Integer.parseInt(request.getParameter("responsableingah").trim());
                             * }
                             */

                            //String codprestadorah = request.getParameter("idprestador");

                            int id_pacienteah = Integer.parseInt(request.getParameter("idpaciente"));


                            band = Integer.parseInt(request.getParameter("band_rips"));

                            int resp = 0;

                            if (band == 1) {

                                try {
                                    resp = ripsInterfase.guardarRipsHospitalizacion(
                                            0,
                                            id_pacienteah,
                                            viaIng,
                                            request.getParameter("fecingah"),
                                            request.getParameter("numero_orden"),
                                            cauExtIng,
                                            request.getParameter("id_dxingah"),
                                            request.getParameter("id_dxsalah"),
                                            request.getParameter("id_dxrel1ah"),
                                            request.getParameter("id_dxrel2ah"),
                                            request.getParameter("id_dxrel3ah"),
                                            request.getParameter("id_dxcomah"),
                                            estSalEgr,
                                            request.getParameter("id_dxmuesalah"),
                                            request.getParameter("fecsalah"),
                                            Integer.parseInt(request.getParameter("idadmision").trim()));

                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            } else {
                                try {
                                    resp = ripsInterfase.actualizarRipsHospitalizacion(
                                            viaIng,
                                            request.getParameter("fecingah"),
                                            request.getParameter("numero_orden"),
                                            cauExtIng,
                                            request.getParameter("id_dxingah"),
                                            request.getParameter("id_dxsalah"),
                                            request.getParameter("id_dxrel1ah"),
                                            request.getParameter("id_dxrel2ah"),
                                            request.getParameter("id_dxrel3ah"),
                                            request.getParameter("id_dxcomah"),
                                            estSalEgr,
                                            request.getParameter("id_dxmuesalah"),
                                            request.getParameter("fecsalah"),
                                            Integer.parseInt(request.getParameter("idadmision").trim()));

                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                            out.println(resp);
                            break;

                        //* **********************rips urgencia******************   
                        case 6:
                            int res = 0;
                            int cauExtIngAu = 0,
                             desSalEgrAu = 0,
                             estSalEgrAu = 0;

                            if (!request.getParameter("cauextingau").equals("")) {
                                cauExtIngAu = Integer.parseInt(request.getParameter("cauextingau").trim());
                            }

                            if (!request.getParameter("destinosalau").equals("")) {
                                desSalEgrAu = Integer.parseInt(request.getParameter("destinosalau").trim());
                            }

                            if (!request.getParameter("estsalau").equals("")) {
                                estSalEgrAu = Integer.parseInt(request.getParameter("estsalau").trim());
                            }

                            String codprestadorau = request.getParameter("idprestador");

                            int id_pacienteau = Integer.parseInt(request.getParameter("idpaciente"));

                            band = Integer.parseInt(request.getParameter("band_rips"));

                            if (band == 1) {
                                try {
                                    res = ripsInterfase.guardarRipsUrgencia(
                                            0,
                                            id_pacienteau,
                                            request.getParameter("fecingau"),
                                            request.getParameter("numeroorden"),
                                            cauExtIngAu,
                                            request.getParameter("id_dxsalau"),
                                            request.getParameter("id_dxrel1au"),
                                            request.getParameter("id_dxrel2au"),
                                            request.getParameter("id_dxrel3au"),
                                            desSalEgrAu,
                                            estSalEgrAu,
                                            request.getParameter("id_dxmuesalau"),
                                            request.getParameter("fecsalau"),
                                            Integer.parseInt(request.getParameter("idadmision").trim()));

                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            } else {

                                try {

                                    res = ripsInterfase.actualizarRipsUrgencia(
                                            request.getParameter("fecingau"),
                                            request.getParameter("numeroorden"),
                                            cauExtIngAu,
                                            request.getParameter("id_dxsalau"),
                                            request.getParameter("id_dxrel1au"),
                                            request.getParameter("id_dxrel2au"),
                                            request.getParameter("id_dxrel3au"),
                                            desSalEgrAu,
                                            estSalEgrAu,
                                            request.getParameter("id_dxmuesalau"),
                                            request.getParameter("fecsalau"),
                                            Integer.parseInt(request.getParameter("idadmision").trim()));

                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }

                            out.println(res);
                            break;

                        //* **********************rips recien nacido******************
                        case 7:

                            String codprestadoran = request.getParameter("idprestador");

                            int id_pacientean = Integer.parseInt(request.getParameter("idpaciente").trim());

                            band = Integer.parseInt(request.getParameter("band_rips"));

                            System.out.println(request.getParameter("id_detalle_admision"));

                            if (band == 1) {

                                try {
                                    ripsInterfase.guardarRipsRecienNacido(numfac,
                                            id_pacientean,
                                            request.getParameter("fecrn"),
                                            request.getParameter("edadges"),
                                            request.getParameter("controlpre"),
                                            request.getParameter("idsexo"),
                                            request.getParameter("peso"),
                                            request.getParameter("id_dxnac"),
                                            request.getParameter("id_dxmu"),
                                            request.getParameter("fecmu"),
                                            request.getParameter("nomrn"),
                                            Integer.parseInt(request.getParameter("id_detalle_admision").trim()));

                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }

                            } else {

                                try {
                                    ripsInterfase.actualizarRipsRecienNacido(
                                            request.getParameter("fecrn"),
                                            request.getParameter("edadges"),
                                            request.getParameter("controlpre"),
                                            request.getParameter("idsexo"),
                                            request.getParameter("peso"),
                                            request.getParameter("id_dxnac"),
                                            request.getParameter("id_dxmu"),
                                            request.getParameter("fecmu"),
                                            request.getParameter("nomrn"),
                                            Integer.parseInt(request.getParameter("id_detalle_admision").trim()));
                                } catch (SQLException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (NamingException ex) {
                                    Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                                }

                            }

                            break;

                    }

                    break;

                case 3:

                    switch (rip) {

                        case 1://retorna el rip de ah     

                            int iddetalleap = Integer.parseInt(request.getParameter("idadmision"));
                            try {
                                planoAHVO = ripsInterfase.getRipsAH(iddetalleap);
                            } catch (SQLException ex) {
                                Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (NamingException ex) {
                                Logger.getLogger(RipsServlet.class.getName()).log(Level.SEVERE, null, ex);
                            }

                            Gson jsPlanoAhh = new Gson();
                            out.println(jsPlanoAhh.toJson(planoAHVO));

                            break;


                        case 2://retorna el rip de au     

                            int idadmisionau = Integer.parseInt(request.getParameter("idadmision").trim());
                            try {
                                planoAUVO = ripsInterfase.getRipsAU(idadmisionau);
                            } catch (SQLException ex) {
                                Logger.getLogger(GestionFacturaServlet.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (NamingException ex) {
                                Logger.getLogger(GestionFacturaServlet.class.getName()).log(Level.SEVERE, null, ex);
                            }

                            Gson jsPlanoAu = new Gson();
                            out.println(jsPlanoAu.toJson(planoAUVO));

                            break;


                        case 3://retorna el rip de ah     

                            int idadmisionah = Integer.parseInt(request.getParameter("idadmision").trim());
                            try {
                                planoAHVO = ripsInterfase.getRipsAH(idadmisionah);
                            } catch (SQLException ex) {
                                Logger.getLogger(GestionFacturaServlet.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (NamingException ex) {
                                Logger.getLogger(GestionFacturaServlet.class.getName()).log(Level.SEVERE, null, ex);
                            }

                            Gson jsPlanoAh = new GsonBuilder().serializeNulls().create();
                            System.out.println(jsPlanoAh.toJson(planoAHVO));
                            out.println(jsPlanoAh.toJson(planoAHVO));

                            break;
                    }
                    break;
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
